package gui.cen.tree;

import core.tree.Vertex;
import core.utils.Utils;
import gui.cen.Center;
import gui.utils.Para;
import gui.utils.Borders;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.LinkedList;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Search extends JPanel implements ActionListener{

    private JTextField text;
    private JButton colLab;
    private Center center;
    public Search(Center center){
        super();
        this.setOpaque(false);
        this.setLayout(new BorderLayout());
        this.center = center;
        text = new JTextField();
        text.setForeground(new Color(178, 178, 178));
        text.setOpaque(false);
        text.setText("Search the tree");
        text.setToolTipText("Comma delimited terms");
        text.setFont(Borders.getFont(Borders.GUIFONT));
        this.add(text, BorderLayout.CENTER);
        this.add(Utils.getButton(colLab = new JButton("Find"), 74, 23, new Color(102, 102, 102), new Color(178, 178, 178), this, "Color Tree"), BorderLayout.EAST);
    } 
    
    public void setText(String str){text.setText("Search");}
    
    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource(); 
        if(source == colLab && Para.TREE!=null){
            String searchText = text.getText().trim();
            String [] searchTerms = searchText.split(" ");
            for(int x=0; x<searchTerms.length; x++){searchTerms[x] = searchTerms[x].trim();}
            LinkedList treeLeaves = Para.TREE.getRoot().getLeafList();
            int foundCounter = 0;
            for(int x=0; x<treeLeaves.size(); x++){
                Vertex v = (Vertex)treeLeaves.get(x);
                boolean matches = true;
                for(int y=0; y<searchTerms.length; y++){if(!v.inTitle(searchTerms[y])){matches = false;}}
                if(matches){
                    v.setHighlight(true);
                    foundCounter++;
                }
                else{v.setHighlight(false);}
            }

            boolean activateColor = false;
            for(int x=0; x<treeLeaves.size(); x++){
                Vertex v = (Vertex)treeLeaves.get(x);
                if(v.getHighlight()==true){
                    activateColor = true;
                    x = treeLeaves.size();
                }
            }

            if(activateColor){
                Color col = JColorChooser.showDialog(this, "Choose Color", new Color(0, 0, 0));
                if(col!=null){
                    for(int x=0; x<treeLeaves.size(); x++){
                        Vertex v = (Vertex)treeLeaves.get(x);
                        if(v.getHighlight()){
                            v.setColOut(col.getRed(), col.getGreen(), col.getBlue());
                            v.setHighlight(false);
                        }
                    }
                }
            }
            center.updateTree();
        }
        else{center.msg("No Tree Available");}
    }
}